package com.four.bergain.service.impl;

import com.alibaba.fastjson.JSON;
import com.four.bergain.dao.BargainMapper;
import com.four.bergain.dao.BargainPropertyMapper;
import com.four.bergain.entity.Bargain;
import com.four.bergain.entity.BargainProperty;
import com.four.bergain.entity.BargainUser;
import com.four.bergain.dao.BargainUserMapper;
import com.four.bergain.feign.UserFeginService;
import com.four.bergain.service.IBargainUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.four.bergain.util.RandomPriceUtils;
import com.guli.common.utils.JwtUtil;
import com.guli.common.utils.R;
import com.guli.common.vo.UserVo;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gs
 * @since 2021-12-20
 */
@Service
public class BargainUserServiceImpl extends ServiceImpl<BargainUserMapper, BargainUser> implements IBargainUserService {

    @Autowired
    private BargainMapper bargainMapper;
    @Autowired
    private BargainPropertyMapper propertyMapper;
    @Autowired
    private BargainUserMapper userMapper;
    @Autowired
    private UserFeginService feginService;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R savePeople(Integer baId, Integer userId) {

        //查看该用户信息
        UserVo vo = feginService.getUserById(userId);
        if (vo==null) {
            return R.error(500,"该用户不存在");
        }
        //查询该用户是否砍过此商品
        BargainUser user = userMapper.getById(userId,baId);
        if (user!=null) {
            return R.error(500,"该用户已经砍过");
        }
        //该商品是否还有库存
        Bargain bargain = bargainMapper.getBargainById(baId);
        if (bargain.getBaLimit()<=0) {
            return R.error(500,"该商品已经买完了");
        }
        //开始添加砍价
        BargainUser bUser = new BargainUser();
        bUser.setBarId(bargain.getBaId());
        bUser.setBarName(bargain.getBaName());
        bUser.setBarImg(bargain.getBaPicture());
        bUser.setUserId(vo.getUserId());
        bUser.setUserName(vo.getUserName());
        //查询砍价商品规格
        BargainProperty property = propertyMapper.getByBaId(bargain.getBaId());
        BigDecimal min = property.getParaPrice().multiply(new BigDecimal(0.90));
        BigDecimal max = property.getParaPrice().multiply(new BigDecimal(0.95));
        //随机值
        BigDecimal add = new BigDecimal(Math.random()).multiply(max.subtract(min)).add(min);
        //砍了多少
        bUser.setBergainPrice(add);
        bUser.setBarTotalCutting(add);
        bUser.setBergainCreateTime(new Date());
        bUser.setPeopleStatus("http://localhost:8001/bargain-people/savePeople/"+userId+"/"+baId+"/");
        bUser.setBarMuch(bargain.getBaQuantity()-1);
        bUser.setBarStatus(0);
        userMapper.saveBargainUser(bUser);
        rabbitTemplate.convertAndSend("order-die",bUser);
        //修改商品砍价人数
        bargain.setBaParticipants(bargain.getBaQuantity()-1);
        bargainMapper.updateBargain(bargain);
        //生成一串随机价格 并存储redis
        List<Double> doubles = RandomPriceUtils.randomPrice(bargain.getBaParticipants(), (int) (property.getParaPrice().subtract(add).doubleValue() * 100));
        if (!doubles.isEmpty()) {
            redisTemplate.opsForValue().set("four:bergain:bargainuser:randomprice", JSON.toJSONString(doubles),1, TimeUnit.DAYS);
        }
        return R.ok("砍价成功，请邀请好友帮您砍价");
    }

    @Override
    public R share(Integer baId, Integer userId) {
        BargainUser byId = userMapper.getById(userId, baId);

        return R.ok().put("bargainUser",byId);
    }
}
